package client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JButton;

import client.ClientView.CharacterTypes;

import common.characters.CharacterInterface;
import common.characters.Ogre;

public class GUIButtonHandler implements ActionListener {
	//
	private final ClientController controller;
	private final JButton diceButton;
	private final JButton connectButton;

	public GUIButtonHandler(final ClientController controller) {
		this.controller = controller;
		this.diceButton = this.controller.getGUI().getMovePanelMoveButton();
		this.connectButton = this.controller.getGUI().getConnectButton();
		this.connectButton.addActionListener(this);
	}

	private CharacterInterface getThisCharacter() {

		if (this.controller.getGUI().getConnectPlayerClass().equals(CharacterTypes.OGRE)) {
			return new Ogre();
		} else {
			return new Ogre();
		}
	}

	@Override
	public void actionPerformed(final ActionEvent e) {
		if ((JButton) (e.getSource()) == (this.connectButton)) {
			try {
				new RMIClient(this.controller, this.controller.getGUI()
						.getConnectServerIP().getText(), this.controller.getGUI()
						.getConnectPlayerName().getText(), getThisCharacter());
				this.controller.getGUI().setConnectLabelVisibility(true);
				this.controller.getGUI().setConnectButtonEnabled(false);
			} catch (final RemoteException a) {
				a.printStackTrace();
			}
		} else if ((JButton) (e.getSource()) == (this.diceButton)) {
			this.controller.diceRolled();
		} else {
			System.out
					.println("Error occured at GUIButtonHanlder Action Performed");
		}

	}

	public void setDiceButtonState(final boolean buttonState) {
		if (buttonState) {
			this.diceButton.addActionListener(this);
			this.diceButton.setEnabled(true);
		} else {
			this.diceButton.removeActionListener(this);
			this.diceButton.setEnabled(false);
		}

	}

}
